import {
    FenceGroup
} from "../models/fence-group.js"

import {
    Judger
} from "../models/judger.js"

Component({
    properties: {
        spu: Object
    },

    data: {
        fences: [],
        judger: Object,
        fenGroup: {}
    },

    observers: {
        spu: function(spu) {
            if (!spu) {
                return
            }
            console.time("init")

            const fenGroup = new FenceGroup(spu)
            this.setData({
                fenGroup
            })
            fenGroup.initFences()

            const judger = new Judger(fenGroup)
            this.setData({
                judger
            })
            wx.setStorageSync("fenGroup", fenGroup)

            this.bindInitData(fenGroup.fences)
			console.timeEnd("init")
        }
    },

    methods: {
        bindInitData(fences) {
            this.setData({
                fences
            })
        },

        // 监听组件cell点击事件
        onCellTab(e) {
            const row = e.detail.row
            const col = e.detail.col
            const cell = e.detail.cell

            //改变当前点击cell状态
            this.data.judger.changeCellStatus(cell, row, col)
            // 重新渲染数据
            const fences = this.data.judger.fenceGroup.fences
            this.setData({
                fences
            })
        }
    }
})